package com.zzb.codegenerator.generator.context;

import com.zzb.codegenerator.config.GeneratorConfig;
import org.apache.commons.lang3.ArrayUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * TableMeta的上下文
 * Created by zhangzhenbin on 17-4-5.
 */
public class TableMetaContext {
    private static Map<String, String> tableMap = new HashMap<>();

    public static Map<String, String> initAndGetTableMap(GeneratorConfig config) {
        //初始化
        initTableMap(config);
        return tableMap;
    }

    public static Map<String, String> getTableMap() {
        return tableMap;
    }

    /**
     * 初始化预构建tableMap
     */
    private static void initTableMap(GeneratorConfig config) {
        if (tableMap == null) tableMap = new HashMap<>();
        String[] tableNames = config.getTableNames().split(",");
        String[] beanNames = config.getBeanNames().split(",");
        if(!ArrayUtils.isSameLength(tableNames,beanNames)){
            System.err.println("ERROR-配置项beanNames和tableNames长度必须对应");
            return;
        }
        if (ArrayUtils.isNotEmpty(tableNames)) {
            for (int i = 0; i < tableNames.length; i++) {
                tableMap.put(tableNames[i], beanNames[i]);
            }
        }
    }
}
